package com.xuecheng.search.listener;

import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.search.constans.MqConstants;
import com.xuecheng.search.po.CourseIndex;
import com.xuecheng.search.service.IndexService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @Author: whopxx
 * @CreateTime: 2025-02-12
 */
@Component
public class MqListener {

    @Value("${elasticsearch.course.index}")
    private String courseIndexStore;

    @Autowired
    IndexService indexService;;

    @RabbitListener(queues = MqConstants.INSERT_QUEUE)
    public void courseInsertOrUpdateListener(CourseIndex courseIndex) {
        Long id = courseIndex.getId();
        if(id==null){
            XueChengPlusException.cast("课程id为空");
        }
        Boolean result = indexService.addCourseIndex(courseIndexStore, String.valueOf(id), courseIndex);
        if(!result){
            XueChengPlusException.cast("添加课程索引失败");
        }
    }

    @RabbitListener(queues = MqConstants.DELETE_QUEUE)
    public void courseDeleteListener(Long id) {

    }
}